package cm.hua.yuan.common.utils;
import android.app.Notification;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 默认 SharedPreferences 工具类，使用时，上下文不能传入空值
 *
 */

public class PreferenceUtils {

    private static String CIRCLE_DEFAULT_NAME = "hy_sign";

    /**
     * 根据NAME获取context中的sp
     *
     * @param c 上下文
     * @return SharedPreferences对象
     */
    private static SharedPreferences getPreferences(Context c) {
        return c.getSharedPreferences(CIRCLE_DEFAULT_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 获取context的sp，根据key和defaultvalue返回String类型值
     * @param key          键
     * @param defaultValue 默认值
     * @return String
     */
    public static String getString(String key, final String defaultValue) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getString(key, defaultValue);
    }

    /**
     * 获取context的sp，根据key返回String类型值
     * @param key     键
     * @return String
     */
    public static String getString(String key) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getString(key, "");
    }

    /**
     * 根据context获取sp，同时根据key向sp中添加数据
     *
     * @param context 上下文
     * @param key     键
     * @param value   值
     */
    public static void setString(final String key, final String value) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        settings.edit().putString(key, value).apply();
    }

    /**
     * 获取context的sp，根据key和defaultValue返回boolean类型值
     *
     * @param context      上下文
     * @param key          键
     * @param defaultValue 默认值
     * @return boolean
     */
    public static boolean getBoolean(final String key, final boolean defaultValue) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getBoolean(key, defaultValue);
    }

    /**
     * 获取context的sp，根据key返回boolean类型
     * @param key     键
     * @return boolean
     */
    public static boolean getBoolean(String key) {

        SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getBoolean(key, false);
    }

    /**
     * 获取context中的sp，判断是否包含key
     * @param key     键
     * @return boolean
     */
    public static boolean hasKey(String key) {
        SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.contains(key);
    }

    /**
     * 获取context中的sp，向其添加线索为key值为value的数据
     * @param key 键
     * @param value 值
     */
    public static void setBoolean(final String key, final boolean value) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        settings.edit().putBoolean(key, value).apply();
    }

    /**
     * 获取context中的sp，向其添加线索为key值为value的数据
     * @param key 键
     * @param value 值
     */
    public static void setInt(final String key, final int value) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        settings.edit().putInt(key, value).apply();
    }

    /**
     * 获取context的sp，根据key和defaultvalue返回int类型值
     * @param key          键
     * @param defaultValue 默认值
     * @return int
     */
    public static int getInt(final String key, final int defaultValue) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getInt(key, defaultValue);
    }

    /**
     * 获取context的sp，根据key返回int类型值
     * @param key 键
     * @return int
     */
    public static int getInt(final String key) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getInt(key, 0);
    }

    /**
     * 获取context中的sp，向其添加线索为key值为value的数据
     *
     * @param context 上下文
     * @param key 键
     * @param value 值
     */
    public static void setFloat(final String key, final float value) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        settings.edit().putFloat(key, value).apply();
    }

    /**
     * 获取context的sp，根据key和defaultvalue返回float类型值
     *
     * @param context      上下文
     * @param key          键
     * @param defaultValue 默认值
     * @return float
     */
    public static float getFloat(final String key, final float defaultValue) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getFloat(key, defaultValue);
    }

    /**
     * 获取context的sp，根据key返回float类型值
     * @param key 键
     * @return float
     */
    public static float getFloat(final String key) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getFloat(key, 0);
    }

    /**
     * 获取context中的sp，向其添加线索为key值为value的数据
     * @param key 键
     * @param value 值
     */
    public static void setSettingLong(final String key, final long value) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        settings.edit().putLong(key, value).apply();
    }

    /**
     * 获取context的sp，根据key和defaultvalue返回long类型值
     * @param key          键
     * @param defaultValue 默认值
     * @return long
     */
    public static long getPrefLong(final String key, final long defaultValue) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getLong(key, defaultValue);
    }

    /**
     * 获取context的sp，根据key返回long类型值
     * @param key          键
     * @return long
     */
    public static long getPrefLong(final String key) {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        return settings.getLong(key, 0);
    }

    /**
     * 在context上创建sp，向其中添线索key内容value
     * @param key 键
     * @param value 值
     */
    public static void setPreferences(String key, Object value) {

        final Editor editor = getPreferences(AppContextManager.getInstance().getContext()).edit();
        if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        }
        editor.apply();
    }


    /**
     * 得到用户设置的消息通知方式，默认为铃声
     * @return int
     */
    public static int getNotification() {

        final SharedPreferences settings = getPreferences(AppContextManager.getInstance().getContext());
        switch (settings.getInt("ring_type", 0)) {
            case 1:
                return Notification.DEFAULT_SOUND;
            case 2:
                return Notification.DEFAULT_VIBRATE;
            case 3:
                return Notification.DEFAULT_ALL;
            default:
                return 0;
        }
    }

    /**
     * 清空所有的值
     */
    public static void clearPreference() {
        final Editor editor = getPreferences(AppContextManager.getInstance().getContext()).edit();
        editor.clear();
        editor.apply();
    }

}
